USE [BMIG_Mensual_POST_CADENA]
GO

/****** Object:  StoredProcedure [dbo].[WASP_M0013_CABECERA_CODIGO_SEGURO]    Script Date: 11/24/2011 11:33:23 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID('WASP_M0013_CABECERA_CODIGO_SEGURO') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[WASP_M0013_CABECERA_CODIGO_SEGURO]
GO

USE [BMIG_Mensual_POST_CADENA]
GO

/****** Object:  StoredProcedure [dbo].[WASP_M0013_CABECERA_CODIGO_SEGURO]    Script Date: 11/24/2011 11:33:23 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

CREATE PROCEDURE [dbo].[WASP_M0013_CABECERA_CODIGO_SEGURO] (@FECHA_INICIO CHAR(8), @FECHA_TERMINO AS CHAR(8)) AS

BEGIN
    SELECT  a.ppcta
          , a.ppoper
          , a.ppsuc
          , a.ppimp11
          , a.ppimp12
          , a.ppimp13
          , a.ppimp14
          , a.ppimp15
          , a.ppimp16
          , a.ppimp17
          , a.ppimp18
          , a.ppimp19
    FROM  dbo.fsd611 AS a WITH (nolock) 
          CROSS JOIN dbo.fsd601 AS b WITH (nolock) 
          CROSS JOIN dbo.JT73109 AS d WITH (nolock)
    WHERE (a.ppexte = 9) 
          AND (a.pgcod = b.pgcod) 
          AND (a.ppmod = b.ppmod) 
          AND (a.ppsuc = b.ppsuc) 
          AND (a.ppmda = b.ppmda) 
          AND (a.ppcta = b.ppcta) 
          AND (a.ppoper = b.ppoper) 
          AND (a.ppsbop = 0) 
          AND (a.pptope = b.pptope) 
          AND (b.d601co = 'S') 
          AND (d.JT73109FPA = a.ppoper) 
          AND (d.JT73109SUC = a.ppsuc) 
          AND (d.JT73109FAP BETWEEN @FECHA_INICIO AND @FECHA_TERMINO) 
          AND (a.ppsbop = b.ppsbop)
    GROUP BY a.ppsuc, a.ppcta, a.ppoper, a.ppimp11, a.ppimp12, a.ppimp13, a.ppimp14, a.ppimp15, a.ppimp16, a.ppimp17, a.ppimp18, a.ppimp19
    
    
    SELECT  a.ppcta
          , a.ppoper
          , a.ppsuc
          , a.ppimp11
          , a.ppimp12
          , a.ppimp13
          , a.ppimp14
          , a.ppimp15
          , a.ppimp16
          , a.ppimp17
          , a.ppimp18
          , a.ppimp19
    into dbo.cabecera_codigos_segurosIP      
    FROM  dbo.cabecera_codigos_segurosIP_aux AS a 
          CROSS JOIN dbo.fsd010 AS c WITH (nolock)
    WHERE (a.ppsuc = c.aosuc) 
      AND (a.ppcta = c.aocta) 
      AND (a.ppoper = c.aooper) 
      AND (c.aosbop = 0) 
      AND (c.aocta <> 999999999) 
      AND (c.aooper <> 999999999)
END
GO


